package com.siwei.eqi.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.siwei.eqi.pojo.entity.IndexCalculationTaskEntity;
import com.siwei.eqi.service.IndexCalculationTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 指数计算任务
 */
@Slf4j
@RestController
@RequestMapping("/IndexCalculationTask")
public class IndexCalculationTaskController {

    @Autowired
    private IndexCalculationTaskService indexCalculationTaskService;

    /**
     * 新增指数计算任务
     * @param entity 新增参数
     * @return 返回成功
     */
    @PostMapping
    public R addIndexCalculationTask(@RequestBody IndexCalculationTaskEntity entity) {
        try {
            entity.setCreatedAt(new Date());
            entity.setUpdatedAt(new Date());
            indexCalculationTaskService.addIndexCalculationTask(entity);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return R.failed(e.getMessage());
        }
        return R.ok(true);
    }

    /**
     * 查询指数计算任务
     * @param taskName 任务名称
     * @param calculationYear 计算年份
     * @param calculationArea 计算区域
     * @param count 当前页
     * @param size 分页条数
     * @return 返回数据
     */
    @GetMapping("/list")
    public R getList(@RequestParam String taskName,@RequestParam String calculationYear,
                     @RequestParam String calculationArea, @RequestParam Long count,@RequestParam Long size) {
        return indexCalculationTaskService.getList(taskName, calculationYear, calculationArea, count, size);
    }

    /**
     * 获取年份
     * @return 年份数据
     */
    @GetMapping("/getYear")
    public R getYear(){
        return R.ok(indexCalculationTaskService.getYear());
    }

    /**
     * 获取计算区域
     * @return 区域信息
     */
    @GetMapping("/getCalculationArea")
    public R getCalculationArea(){
        return R.ok(indexCalculationTaskService.getCalculationArea());
    }



}
